#include "../include/calculator.h"
#include <cmath>
#include <stdexcept>

namespace math {

Calculator::Calculator() {
    // Constructor implementation
}

Calculator::~Calculator() {
    // Destructor implementation
}

double Calculator::add(double a, double b) {
    return a + b;
}

double Calculator::subtract(double a, double b) {
    return a - b;
}

double Calculator::multiply(double a, double b) {
    return a * b;
}

double Calculator::divide(double a, double b) {
    if (!validateDivision(b)) {
        throw std::invalid_argument("Division by zero is not allowed");
    }
    return a / b;
}

double Calculator::power(double base, int exponent) {
    return computePower(base, exponent);
}

double Calculator::squareRoot(double value) {
    if (value < 0) {
        throw std::invalid_argument("Cannot compute square root of negative number");
    }
    return std::sqrt(value);
}

bool Calculator::validateDivision(double divisor) {
    return divisor != 0;
}

double Calculator::computePower(double base, int exponent) {
    if (exponent == 0) {
        return 1.0;
    }
    
    if (exponent < 0) {
        return 1.0 / computePower(base, -exponent);
    }
    
    if (exponent % 2 == 0) {
        double half = computePower(base, exponent / 2);
        return half * half;
    } else {
        return base * computePower(base, exponent - 1);
    }
}

} // namespace math 